/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fpgrowth_demo;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author TTIN
 */
public class Database {
    public ArrayList<ArrayList<Integer>> origin_data = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> sorted_data = new ArrayList<ArrayList<Integer>>();
    public Hashtable<Integer,Integer> item_count = new Hashtable<Integer,Integer>();
    public String file_name = new String();
    public double MinSupp=0;

    public boolean readFromFile(String filename)
    {
        try
        {
            origin_data.clear();
            sorted_data.clear();
            item_count.clear();

            File f = new File(filename);
            if (!f.exists()) return false;
            Scanner scanner = new Scanner(f);
            while (scanner.hasNextLine())
            {
                Scanner line_scanner = new Scanner(scanner.nextLine());
                ArrayList<Integer> Transaction = new ArrayList<Integer>();
                while (line_scanner.hasNextInt())
                {
                    Transaction.add(line_scanner.nextInt());
                }
                origin_data.add(Transaction);
            }
            return true;
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public void preprocess()
    {
        for (int i=0;i<origin_data.size();i++)
            for (int j=0;j<origin_data.get(i).size();j++)
            {
                Integer item = new Integer(origin_data.get(i).get(j));
                if (item_count.containsKey(item))
                {
                    Integer count = new Integer(item_count.get(item));
                    count++;
                    item_count.remove(item);
                    item_count.put(item, count);
                }
                else
                {
                    item_count.put(item, 1);
                }

            }
        //copy
        for (ArrayList<Integer> i:origin_data)
        {
            ArrayList<Integer> newI=(ArrayList<Integer>)i.clone();//new ArrayList<Integer>();
            sorted_data.add(newI);
        }

        //prune
        for (ArrayList<Integer> i:sorted_data)
        {
            for (int j=0;j<i.size();j++)
            {
                int item = i.get(j);
                double supp = item_count.get(item).doubleValue()/(sorted_data.size());

                if (supp<MinSupp)
                {
                    i.remove(j);
                    j--;
                }
            }
        }

        //sort
        for (ArrayList<Integer> i:sorted_data)
        {
            for (int j=0;j<i.size();j++)
                for (int k=j+1;k<i.size();k++)
                    if ((item_count.get(i.get(j))<item_count.get(i.get(k))) ||
                        ((item_count.get(i.get(j))==item_count.get(i.get(k))) &&
                        (i.get(j)>i.get(k))))
                    {
                        int t = i.get(j);
                        i.set(j, i.get(k));
                        i.set(k, t);
                    }
        }

    }

    public void writeDatabaseToList(JList list1, JList list2)
    {
        DefaultListModel model=new DefaultListModel();
        for (int i=0;i<origin_data.size();i++)
        {
            String s = new String();
            for (int j=0;j<origin_data.get(i).size();j++)
                if (j==0)
                    s = Integer.toString(origin_data.get(i).get(j));
                else
                    s = s.concat(","+Integer.toString(origin_data.get(i).get(j)));
            model.addElement(s);
        }
        list1.setModel(model);

        model = new DefaultListModel();
        for (int i=0;i<sorted_data.size();i++)
        {
            String s = new String();
            for (int j=0;j<sorted_data.get(i).size();j++)
                if (j==0)
                    s = Integer.toString(sorted_data.get(i).get(j));
                else
                    s = s.concat(","+Integer.toString(sorted_data.get(i).get(j)));
            if (s.length()==0)
                s = s+" ";
            model.addElement(s);
        }
        list2.setModel(model);
    }

    void setMinSupport(double minsupp)
    {
        MinSupp = minsupp;
    }

}
